<template>
	<view class="wrap">
		<js-error mode="bottom" ref="jsError"></js-error>
		<view v-if="showflag == 1" class="search">
			<u-search v-model="barCode" ref="uSearch" :focus="focus" placeholder="扫一扫" :show-action="false"
				@custom="search" @search="search"></u-search>
		</view>
		<view style="padding: 0px 10px;">
			<view  style="margin-bottom: 5px;"><text class="">存货编码：</text>{{model.basInv.cinvcode}}</view>
			<view style="margin-bottom: 5px;"><text class="">存货代码：</text>{{model.basInv.cinvaddcode}}</view>
			<view style="margin-bottom: 5px;"><text class="">存货名称：</text>{{model.basInv.cinvname}}</view>
			<view style="margin-bottom: 5px;"><text class="">仓库：</text>{{model.cwhcode}}</view>
			<view style="margin-bottom: 5px;"><text class="">下架数量：</text>{{model.iqty}}</view>
			<view style="margin-bottom: 5px;"><text class="">累计下架数量：</text>{{model.pickQty}}</view>
		</view>
		<scroll-view class="scroll-list bg-white" scroll-y="true" :style="[{height:stockListHeight + 'px'}]">
			<u-table>
				<u-tr>
					<!-- <u-th width="50px" v-if="showflag == 1"></u-th> -->
					<u-th>货位</u-th>
					<u-th>批次</u-th>
					<u-th>数量</u-th>
					<u-th>验证</u-th>
				</u-tr>
				<view style=" height: 50vh;overflow: scroll;">
					<view v-for="(item,index) in list" :key='index'>
						<u-tr v-if="list">
							<!-- <u-td width="50px" v-if="showflag == 1">
								<u-checkbox style="display: flex;justify-content: center;"  @change="checkboxChange" v-model="item.checked"></u-checkbox>
							</u-td> -->
							<u-td style="word-break: break-all;"> {{item.basPos.treeNames}}</u-td>
							<u-td>{{item.cbatch}}</u-td>
							<u-td>{{item.iqty}}</u-td>
							<u-td>
								<view v-if="item.bcheck == '0'">
									<u-button  type="primary" @click="search(item)" >扫一扫</u-button>
								</view>
								<view v-else>已验证</view>
							</u-td>
						</u-tr>
					</view>
					<u-empty v-if="!list" text="暂无数据" mode="data"></u-empty>
				</view>

			</u-table>
		</scroll-view>
		<view class="footer">
			<view class="confrim-btn padding padding-bottom-sm flex bg-white">
				<u-button  type="primary" class="btn" style="width: 100%;" @click="submit">提交</u-button>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				showflag: 0,
				stockListHeight: 0,
				barcode:'',//AA01|2W0243080000I999|JE493ZLQ4-DS|1029U230729HT0200500007|001|5642|1|202303301121|0AAA|M040006||||||||||
				model:{
					basInv:{},
					djno:{},
					iqty:'',
					pickQty:'',
					list:[],
				},
				list: [],
				flag: '1',
				id:'',
			}
		},
		
		onShow() {},
		onLoad(params) {
			this.showflag = params.showflag ? params.showflag : 0
			this.id = params.id;
			var _self = this;
			uni.getSystemInfo({
				success: (e) => { // resu 可以获取当前屏幕的高度
					_self.stockListHeight = e.windowHeight - uni.upx2px(450);
				},
				fail: (res) => {}
			});
			this.loadData();
		},
		methods: {
			loadData(){
				this.$u.api.asdApi.getLl({id:this.id}).then(res => {
					this.model = res.data;
					this.list = res.data.list;
				})
			},
			
			search(item){
				var _this = this;
				uni.scanCode({ 
					onlyFromCamera: true,
					success: function(res) {
						this.loadCheckData(item,res.result);
					}.bind(this)
				});
				// this.loadCheckData(item,this.barcode);
			},
			
			loadCheckData(item,barcode){
				this.$u.api.asdApi.checkLlByCode({barcode:barcode,id:item.id}).then(res => {
					if(res.result == 'false'){
						this.$refs.jsError.showError('',res.message,'error');
					}else{
						item.bcheck = 1;
					}
				})
			},
			
			// 提交
			async submit() {
				var i = await this.checkData();
				let data = {
					...this.model,
					list:this.list,
				}
				if(i == this.list.length){
					this.$u.api.asdApi.downToU8(data).then(res => {
						uni.showModal({
							title: '提示',
							content: res.message,
							showCancel: false,
							success: (res) => {}
						});
						if(res.result == 'true'){
							uni.navigateBack({
								delta: 1,	
							})
						}
					})
				}else{
					this.$refs.jsError.showError('','请验证现品票！','error');
				}
			},
			
			checkData(){
				const _this = this;
				return new Promise((resolve,reject) =>{
					var i = 0;
					this.list.forEach(item=>{
						if(item.bcheck == '1'){
							i++;
						}
					})
					resolve(i);
				})
			},
		}
	}
</script>

<style scoped>
	@import '../asd.css';
	/deep/ .u-checkbox__label{
		display: none ;
	}
</style>